package main

import (
	"fmt"
	"math"
	"strconv"
	"strings"
)

func BinaryFormat2(num int32) string {
	if num < 0 {
		return fmt.Sprintf("请输入正数")
	}
	var l = 32
	var binN string
	for n := int(num); n > 0; n /= 2 {
		temp := n % 2
		binN = strconv.Itoa(temp) + binN
	}
	strNum := l - len(binN)
	str := ""
	if strNum > 0 {
		for i := 0; i < strNum; i++ {
			str += "0"
		}
	}
	return str + binN
}

func BinaryFormat1(n int32) string {
	a := uint32(n)
	sb := strings.Builder{}
	c := uint32(math.Pow(2, 31))
	if n >= 0 {
		for i := 0; i < 32; i++ {
			if a&c != 0 {
				sb.WriteString("1")
			} else {
				sb.WriteString("0")
			}
			//c>>=1
			c = c / 2
		}
	} else {
		var msg string = `
		int    #有符号的    存在负数
		uint   #无符号的    没有负数，使用类型转换会有意想不到的错误
		unit范围大小：
		uint8. [0, 255]
		uint16. [0, 65535]
		uint32. [0, 4294967295]
		uint64. [0, 18446744073709551615]
		`
		fmt.Println("温馨提示：", msg)
		fmt.Printf("%d %v %t %T", a, a, a, a)
	}

	return sb.String()
}

func main() {
	var a int32 = 8
	var str string
	str = BinaryFormat1(a)
	fmt.Println(str)
	fmt.Println(BinaryFormat1(999))
	fmt.Println(BinaryFormat1(-999))
	fmt.Println(BinaryFormat1(-8))

	fmt.Println(BinaryFormat2(666))
	fmt.Println(BinaryFormat2(-888))
	fmt.Println(BinaryFormat2(-8))

}
